﻿using System;
using System.Text;
using System.Linq;
using static System.Console;
using System.IO;
namespace regressive_analysis
{
    internal class Program
    {
        public static double[] data=new double[66];
        public static void Read_from_txt()
        {
           TextReader reader = File.OpenText("E:/gdp_data.txt");
            for (int i = 0; i < 31; i++)
            {
                string str = reader.ReadLine();
                data[i] = Convert.ToDouble(str);
                //WriteLine(data[i]);
            }
        }

        public static double Avr_y()
        {
            double summ = 0;
            double avrr = 0;
            for (int i = 0; i <23; i++)
            {
                summ += data[i];
            }
            avrr = summ / 23;
            return avrr;
        }

        public static double Avr_x()
        {
            double summ = 0;
            double avrr = 0;
            for (int i =1998 ; i < 2021; i++)
            {
                summ += i;
            }
            
            avrr = summ / 23;
            return avrr;
        }

        public static double Get_numerator(double avrr_x,double avrr_y)
        {
            double res = 0;
            double temp = 0;
            for (int i = 0; i < 23; i++)
            {
                temp = (data[i] - avrr_y) * (i + 1998 - avrr_x);
                //WriteLine(i+1990-avrr_x);
                res += temp;
                
            }
            return res;
        }

        public static double Get_denominator(double avrr_x)
        {
            double res = 0;
            double temp = 0;
            for (int i = 1998; i < 2021; i++)
            {
                temp = (i -avrr_x)*(i- avrr_x);
                res += temp;
                
            }
            return res;
        }
        public static void Main(string[] args)
        {
            Read_from_txt();
            double average_x = Avr_x();
            double average_y = Avr_y();
            double numer=Get_numerator(average_x, average_y);
            double denom=Get_denominator(average_x);
            double b = numer / denom;
            double a = average_y - b * average_x;
            double fin_res = b * 2021+a;
            Write("2021年预期gdp是{0}亿美元",fin_res);
            
        }
    }
}